package top.fzqblog.firstapp;

import android.animation.ObjectAnimator;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.Toast;

/**
 * Created by 抽离 on 2017/8/29 0029.
 */

public class AnimationTest extends AppCompatActivity implements View.OnClickListener{

    private ImageView imageView;

    private Animation animation;

    private AnimationSet animationSet;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.animationtest);
        imageView = (ImageView) findViewById(R.id.animation_iv);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(AnimationTest.this, "点击了图片", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.rotateTest:
                animation =
                        new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_PARENT,
                                0, Animation.RELATIVE_TO_PARENT, 0.5f);
                animation.setDuration(2000);
                imageView.startAnimation(animation);
                break;
            case R.id.scaleTest:
                animation =
                        new ScaleAnimation(1f, 0.5f, 1f, 0.5f,
                                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                animation.setStartOffset(1000);
                animation.setDuration(2000);
                animation.setFillBefore(false);
                imageView.startAnimation(animation);
                break;
            case R.id.alphaTest:
                animation = new AlphaAnimation(1f, 0.1f);
                animation.setDuration(2000);
                imageView.startAnimation(animation);
                break;
            case R.id.translateTest:
            animation = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0.5f,
                            Animation.RELATIVE_TO_PARENT, 0f, Animation.RELATIVE_TO_PARENT, 0f
                        );
//                animation = new TranslateAnimation(0f, 0.1f, 0f, 0.5f);
            animation.setInterpolator(new AccelerateInterpolator());
            animation.setDuration(2000);
            animation.setFillAfter(true);
            imageView.startAnimation(animation);
            break;
            case R.id.groupTest:
                animationSet =new AnimationSet(true);
                ScaleAnimation scaleAnimation = new ScaleAnimation(0.1f, 1f, 0.1f, 1f,
                        Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF , 0.5f);
                scaleAnimation.setDuration(3000);
                AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
                alphaAnimation.setDuration(3000);
                alphaAnimation.setStartOffset(3000);
                animationSet.addAnimation(scaleAnimation);
                animationSet.addAnimation(alphaAnimation);
                animationSet.setInterpolator(new DecelerateInterpolator());
                imageView.startAnimation(animationSet);
                break;
            case R.id.rotateXmlTest:
                Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotatetest);
                animation.setDuration(2000);
                imageView.startAnimation(animation);
                break;
            case R.id.scaleXmlTest:
                animation = AnimationUtils.loadAnimation(this, R.anim.scaletest);
                animation.setDuration(2000);
                imageView.startAnimation(animation);
                break;
            case R.id.xmlAlphaTest:
                animation = AnimationUtils.loadAnimation(this, R.anim.alphatest);
                animation.setDuration(2000);
                imageView.startAnimation(animation);
                break;
            case R.id.xmlTranslateTest:
                animation = AnimationUtils.loadAnimation(this, R.anim.translatetest);
                animation.setDuration(2000);
                imageView.startAnimation(animation);
                break;
            case R.id.xmlGroupTest:
                animation = AnimationUtils.loadAnimation(this, R.anim.grouptest);
                imageView.startAnimation(animation);
                break;
            case R.id.btn_fadeinout:
                startActivity(new Intent(this, AnimationPage.class));
                overridePendingTransition(R.anim.fadein, R.anim.fadeout);
                break;
            case R.id.btn_rightinleftout:
                startActivity(new Intent(this, AnimationPage.class));
                overridePendingTransition(R.anim.rightin, R.anim.leftout);
                break;
            case R.id.btn_bottomintopout:
                startActivity(new Intent(this, AnimationPage.class));
                overridePendingTransition(R.anim.bottomin, R.anim.topout);
                break;
            case R.id.rotatein:
                startActivity(new Intent(this, AnimationPage.class));
                overridePendingTransition(R.anim.rotatein, R.anim.fadeout);
                break;
            case R.id.btn_objectTranslateTest:
                ObjectAnimator.ofFloat(imageView, "translationX", 1f, 200f).setDuration(2000).start();
                break;
        }
    }
}
